<?php // no direct access
defined('_JEXEC') or die('Restricted access');

$db =& JFactory::getDBO();

if (!function_exists('filternewslettersmarttext')) {
  include('administrator/components/com_nspro/lib.php');
}

function emailNotInDB($email) {
  $db =& JFactory::getDBO();
  $query = 'SELECT * FROM `#__nspro_subs` WHERE `email` = "'.mysql_escape_string($email).'"';
  $db->setQuery($query);
  $myResult = $db->loadObject();
  
  if (!is_null($myResult)) {
    return ($myResult->email == "");
  }
  else {
    return true;
  }
}

$uredirectToPage = $this->uredirectToPage;
$uredirectPage = $this->uredirectToPage;

$myEmailLabel = $this->myEmailLabel;
$recipient = $this->recipient;

$buttonText = $this->buttonText;
$notifyUnsub = $this->notifyUnsub;
$subject = $this->subject;
$fromName = $this->fromName;
$fromEmail = $this->fromEmail;
$fromNameConfirmun = $this->fromNameConfirmun;
$fromEmailConfirmun = $this->fromEmailConfirmun;
$subjectConfirmun = $this->subjectConfirmun;
$sendingWithSetEmail = $this->sendingWithSetEmail;
$emailWidth = $this->emailWidth;
$buttonWidth = $this->buttonWidth;
$unsub_completed_color = $this->unsub_completed_color;
$unsub_error_color = $this->unsub_error_color;
$unsub_pre_text = $this->unsub_pre_text;

$url = JRoute::_('index.php?option=com_nspro&view=nspro&layout=unsubscribe');
$confirm_url = 'index.php?option=com_nspro&view=nspro&layout=confirmunsub';


print '<div class="nspro componentheading">'.JText::_('Unsubscribe from Newsletter').'</div><br/>' .
      '<div id="nspro_unsub_intro">' . $unsub_pre_text . '</div><br/>' .
      '<div class="nspro unsubscribe">';
if (isset($_POST["un_email"])) {
  if ($_POST["un_email"] === "") {
    $myReplacement = '<span style="color: '.$unsub_error_color.';">' . JText::_('No email specified') . '</span>' .
                     '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                     '<table cellpadding="2" border="0"><tr style="border: none;"><td style="border: none; padding: 2px;">' . $myEmailLabel . '</td><td style="border: none; padding: 2px;"><input class="nspro unsubscribe inputbox" type="text" name="un_email" size="' . $emailWidth . '"/></td></tr>' . "\n" .
                     '<tr style="border: none;"><td colspan="2" style="border: none;"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: '.$buttonWidth.'%;"/></td></tr></table cellpadding="2"></form></div>' . "\n";
    print $myReplacement;
    // closing div nspro unsubscribe
    print '</div>';
    return true;
  }
  if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST["un_email"])) {
    $myReplacement = '<span style="color: '.$unsub_error_color.';">' . JText::_('Invalid email specified') . '</span>' .
                     '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                     '<table cellpadding="2"><tr style="border: none;"><td style="border: none; padding: 2px;">' . $myEmailLabel . '</td><td style="border: none; padding: 2px;"><input class="nspro unsubscribe inputbox" type="text" name="un_email" size="' . $emailWidth . '"/></td></tr>' . "\n" .
                     '<tr style="border: none;"><td colspan="2" style="border: none;"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: '.$buttonWidth.'%;"/></td></tr></table cellpadding="2"></form></div>' . "\n";
    print $myReplacement;
    // closing div nspro unsubscribe
    print '</div>';    
    return true;
  }
  if (emailNotInDB($_POST["un_email"])) {
    $myReplacement = '<span style="color: '.$unsub_error_color.';">' . JText::_('Email not found in subscriber list') . '</span>' .
                     '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                     '<table cellpadding="2"><tr style="border: none;"><td style="border: none; padding: 2px;">' . $myEmailLabel . '</td><td style="border: none; padding: 2px;"><input class="nspro unsubscribe inputbox" type="text" name="un_email" size="' . $emailWidth . '"/></td></tr>' . "\n" .
                     '<tr style="border: none;"><td colspan="2" style="border: none;"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: '.$buttonWidth.'%;"/></td></tr></table cellpadding="2"></form></div>' . "\n";
    print $myReplacement;
    // closing div nspro unsubscribe
    print '</div>';
    return true;
  }

  if ($this->confirmUnsub === '0') {

    if ($notifyUnsub != '0') {
      $mailSender = &JFactory::getMailer();
      $mailSender->addRecipient($recipient);

      $query = 'SELECT * FROM `#__nspro_subs` WHERE `email` = "'.mysql_escape_string($email).'"';
      $db->setQuery($query);
      $myResult = $db->loadObject();

      if ($sendingWithSetEmail) {
        $mailSender->setSender(array($fromEmail,$fromName));
      }
      else {
        $mailSender->setSender(array($_POST["un_email"],$myResult->name));
      }

      $myMessage = $myEmailLabel . ' ' . $_POST["un_email"] . ', ' . date("r");

      $mailSender->setSubject($subject);
      $mailSender->setBody($myMessage);

      if (!$mailSender->Send()) {
        $myReplacement = '<span style="color: '.$unsub_error_color.';">' . JText::_('Your unsubscription was not completed') . '</span>';
        print $myReplacement;
      }
      else {
        if (!$uredirectToPage) {
          $myReplacement = '<span style="color: '.$unsub_completed_color.';">' . JText::_('Your unsubscription was completed') . '</span>';
          print $myReplacement;
        }
        else {
          header("Location: ".$uredirectPage);
        }
      }

    }
    else {
      if (!$uredirectToPage) {
        $myReplacement = '<span style="color: '.$unsub_completed_color.';">' . JText::_('Your unsubscription was completed') . '</span>';
        print $myReplacement;
        print '</div>';
      }
      else {
        header("Location: ".$uredirectPage);
      }

    }

    $query = 'DELETE FROM `#__nspro_subs` WHERE `email` = "'.mysql_escape_string($_POST["un_email"]).'"';
    $db->setQuery($query);
    if (!$db->query()) {
      print JText::_('Database Deletion Error. Please try again');
      print '</div>';
      return;
    }
  }
  else {

    $uri = & JURI::getInstance();
    $myBase = JURI::base();
    $myMessage = '<a href="'.$myBase.$confirm_url.'&u='.md5($_POST["un_email"]).'">'.JText::_('Click to complete your Unsubscription').
                 "</a>\n<br/>\n<br/>".JText::_('or copy this to your address bar')."\n<br/>\n<br/>".$myBase.$confirm_url.'&u='.md5($_POST["un_email"]);
    if (MySendMail($fromEmailConfirmun, $fromNameConfirmun, $_POST["un_email"], $subjectConfirmun, $myMessage, true) !== true) {
      $myReplacement = '<span style="color: '.$unsub_error_color.';">' . JText::_('Your unsubscription could not be completed') . '</span>';
    }
    else {
      $myReplacement = '<span style="color: '.$unsub_completed_color.';">' . JText::_('An email has been sent to you to confirm unsubscription') . '</span>';
    }
    print $myReplacement;
    print '</div>';

  }

  return true;
}

if ($recipient === "") {
  $myReplacement = '<span style="color: '.$unsub_error_color.';">No recipient specified</span>';
  print $myReplacement;
  return true;
}

$myReplacement = '<div class="nspro unsubscribe"><form action="' . $url . '" method="post">' . "\n" .
                 '<table cellpadding="2"><tr style="border: none;"><td style="border: none; padding: 2px;">' . $myEmailLabel . '</td><td style="border: none; padding: 2px;"><input class="nspro unsubscribe inputbox" type="text" name="un_email" size="' . $emailWidth . '"/></td></tr>' . "\n" .
                 '<tr style="border: none;"><td colspan="2" style="border: none;"><input class="nspro unsubscribe button" type="submit" value="' . $buttonText . '" style="width: '.$buttonWidth.'%;"/></td></tr></table cellpadding="2"></form></div>' . "\n";
print $myReplacement;


print '</div>';

?>